package com.zhilian.hzrf_oa.mvp.login.model;

import com.colin.constant.Constants;
import com.colin.http.HttpServiceManager;
import com.colin.utils.LogUtil;

import org.json.JSONObject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * Created by colin on 2018/2/8 9:25 .
 */

public class LoginModel implements ILoginModel {
	@Override
	public void login(String userName, String passWord, Callback callback) {
		HttpServiceManager.getInstance()
			.getHttpService()
			.login(userName + "-" + passWord)
			.subscribeOn(Schedulers.newThread())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Consumer<ResponseBody>() {
				@Override
				public void accept(ResponseBody responseBody) throws Exception {
					JSONObject dataJson = new JSONObject(responseBody.string());
					String status = dataJson.getString("status");
					if (status.equals("1")) {
						Constants.accessToken = dataJson.getString("msg");
						Constants.userName = dataJson.getString("uname");
						Constants.limit = dataJson.getString("startReceive");//服务器发出：0，无权限，1，有权限
						callback.onSuccess();
					}else {
						LogUtil.e("status = "+status);
					}
				}
			},new Consumer<Throwable>() {
				@Override
				public void accept(Throwable throwable) throws Exception {
					LogUtil.e(throwable.getMessage());
				}
			});

	}
}
